﻿using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace start
{
    /// <summary>
    /// Upgrade.xaml 的交互逻辑
    /// </summary>
    public partial class Upgrade : Window
    {
        public bool Success = false;

        public Upgrade()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.txt_current.Text = Startup.CurrentVersion;
            this.txt_lastest.Text = Startup.LastestVersion;
            this.txt_list.Text = Startup.UpgradeFileList;
        }
       
        public void Download(string name)
        {
            try
            {
                Log("开始下载" + name);
                WebClient client = new WebClient();
                string serverURL = ConfigurationManager.AppSettings["serverURL"];
                client.DownloadFile(serverURL + name, name);
                Log("下载" + name + "完毕");
            }
            catch (Exception ex)
            {
                Log("下载" + name + "失败," + ex.Message);
            }
        }

        private void btn_run_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(DoUpgrade);
            thread.Start();
        }

        private void DoUpgrade()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                this.btn_run.IsEnabled = false;
            });

            try
            {
                string[] array = Startup.UpgradeFileList.Split('\n');
                foreach (string str in array)
                {
                    if (string.IsNullOrWhiteSpace(str))
                    {
                        continue;
                    }
                    char c = str.ToCharArray()[0];
                    string name = str.Substring(1).Trim();
                    if (c == '+')
                    {
                        Del(name);
                        Download(name);
                    }
                    if (c == '-')
                    {
                        Del(name);
                    }
                }

                if (File.Exists("checklist.txt"))
                {
                    File.Delete("checklist.txt");
                }
                Log("更新完毕");
                Success = true;
            }
            catch (Exception ex)
            {
                Log("更新失败" + ex);
                Success = false;
            }

            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                this.btn_run.IsEnabled = true;
            });
        }

        private void Log(string str)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                this.txt_log.Text += str + "\n";
            });
            Thread.Sleep(500);
        }

        private void Del(string name)
        {
            if(File.Exists(name))
             {
                Log("开始删除" + name);
                File.Delete(name);
                Log("删除" + name + "完毕");
            }
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
